package com.kzy.demoadmin.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kzy.demoadmin.web.entity.Book;
import com.kzy.demoadmin.web.entity.Reserve;
import com.kzy.demoadmin.web.entity.User;
import com.kzy.demoadmin.web.mapper.ReserveMapper;
import com.kzy.demoadmin.web.service.BookService;
import com.kzy.demoadmin.web.service.ReserveService;
import com.kzy.demoadmin.web.utils.JwtTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ReserveServiceImpl implements ReserveService {

    @Autowired
    private ReserveMapper reserveMapper;

    @Autowired
    private BookService bookService;

    /**
     * 查询所有借阅记录
     *
     * @return
     */
    @Override
    public PageInfo<Reserve> findAll(Reserve reserve) {
        PageHelper.startPage(reserve.getPageNo(), reserve.getPageSize());
        List<Reserve> list = reserveMapper.findAll(reserve);
        return new PageInfo<>(list);
    }

    /**
     * 根据ID查询借阅记录
     *
     * @param id
     * @return
     */
    @Override
    public Reserve findById(Integer id) {
        return reserveMapper.findById(id);
    }

    /**
     * 新增借阅记录
     *
     * @param book
     */
    @Transactional// 开启事务
    @Override
    public void add(Book book) {
        //编辑书籍
        bookService.edit(book);
        Reserve reserve = new Reserve();
        reserve.setBookId(book.getId());
        // 设置借阅时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedNow = now.format(formatter);
        reserve.setTime(formattedNow);
        //获取当前人的ID
        User user = JwtTokenUtils.getCurrentUser();
        reserve.setUserId(user.getId());
        //设置状态-默认未归还
        reserve.setStatus(0);
        reserveMapper.insert(reserve);
    }

    /**
     * 更新借阅记录
     *
     * @param reserve
     */
    @Transactional// 开启事务
    @Override
    public void update(Reserve reserve) {
        Book book = bookService.findById(reserve.getBookId());
        book.setNum(book.getNum() + 1);
        //归还对应的书籍并且设置数量+1
        bookService.edit(book);
        reserveMapper.update(reserve);
    }

    /**
     * 删除借阅记录
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        return reserveMapper.deleteById(id);
    }

    /**
     * 根据用户ID查询借阅记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<Reserve> findByUserId(Integer userId) {
        return reserveMapper.findByUserId(userId);
    }

    /**
     * 根据书籍ID查询借阅记录
     *
     * @param bookId
     * @return
     */
    @Override
    public List<Reserve> findByBookId(Integer bookId) {
        return reserveMapper.findByBookId(bookId);
    }
}